﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using System.Xml;
using System.Web;
using System.Reflection;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 单个权限维度信息
    /// </summary>
    public class Dimension : AbstractConvertObject,IConvertXml
    {
        #region 属性
        private DimensionLevels _dimensionlevels = null;
        private ACItems _acitems = null;
        private string _id = "";
        private bool _hierachical =false;
        private bool _multicheck = true;
        private ACSection _acsection = null;
        private string _display = "";

        /// <summary>
        /// 权限层次列表
        /// </summary>
        public DimensionLevels DimensionLevels
        {
            get
            {
                return _dimensionlevels;
            }
            set
            {
                _dimensionlevels = value;
            }
        }
        /// <summary>
        /// 权限项列表
        /// </summary>
        public ACItems ACItems
        {
            get
            {
                return _acitems;
            }
            set
            {
                _acitems = value;
            }
        }
        /// <summary>
        /// 权限维度的标识
        /// </summary>
        public string ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        /// <summary>
        /// 是否包含多层次权限项
        /// TRUE/FALSE
        /// </summary>
        public bool Hierachical
        {
            get
            {
                return _hierachical;
            }
            set
            {
                _hierachical = value;
            }
        }
        /// <summary>
        /// 否允许选择多个权限项，TRUE/FALSE
        /// </summary>
        public bool MultiCheck
        {
            get
            {
                return _multicheck;
            }
            set
            {
                _multicheck = value;
            }
        }
        /// <summary>
        /// 所属的纬度区域
        /// </summary>
        public ACSection ACSection
        {
            get
            {
                return _acsection;
            }
            set
            {
                _acsection = value;
            }
        }
        /// <summary>
        /// 显示的名称
        /// </summary>
        public string Display
        {
            get
            {
                return _display;
            }
            set
            {
                _display = value;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                //1.添加Dimension基本attributes
                _xml.Append("<Dimension");
                _xml.Append(" ID=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.ID )+ "\"");
                _xml.Append(" Display=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Display )+ "\"");
                _xml.Append(" Hierachical=\"" + this.Hierachical.ToString().ToLower() + "\"");
                _xml.Append(" MultiCheck=\"" + this.MultiCheck.ToString().ToLower() + "\"");
                _xml.Append(" ACSection=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.ACSection.ID )+ "\"");
                _xml.Append(" >");

                //2.添加DimensionLevels xml字符串
                _xml.Append(this.DimensionLevels.XML);
                //3.添加ACItems xml字符串
                _xml.Append(this.ACItems.XML);

                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</Dimension>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            this.ID = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["ID"].InnerText);
            this.Display = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Display"].InnerText);
            string strHierachical = this.CurrentXmlNode.Attributes["Hierachical"].InnerText;
            if (strHierachical.ToLower() == "false")
            {
                this.Hierachical = false;
            }
            else
            {
                this.Hierachical = true;
            }
            string strMultiCheck = this.CurrentXmlNode.Attributes["MultiCheck"].InnerText;
            if (strMultiCheck.ToLower() == "false")
            {
                this.MultiCheck = false;
            }
            else
            {
                this.MultiCheck = true;
            }

            this.ACSection = new ACSection();
            this.ACSection.ID = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["ACSection"].InnerText);

            //DimensionLevels
            this.DimensionLevels = new DimensionLevels();
            this.DimensionLevels.CurrentXmlNode = this.CurrentXmlNode.SelectSingleNode("DimensionLevels");
            this.DimensionLevels = (DimensionLevels)this.DimensionLevels.ConvertToObject();

            //ACItems
            this.ACItems = new ACItems(this);
            this.ACItems.CurrentXmlNode = this.CurrentXmlNode.SelectSingleNode("ACItems");
            this.ACItems = (ACItems)this.ACItems.ConvertToObject();
            #region 注销
            //this.ACItems = new ACItems();
            //foreach (XmlNode xn in this.CurrentXmlNode.SelectSingleNode("ACItems").ChildNodes)
            //{
            //    ACItem acitem = new ACItem();
            //    acitem.ID = xn.Attributes["ID"].InnerText;
            //    acitem.Display = xn.Attributes["Display"].InnerText;

            //    acitem.DimensionLevel = new DimensionLevel();
            //    acitem.DimensionLevel.ID = xn.Attributes["DimensionLevel"].InnerText;
            //    foreach (object obj in this.DimensionLevels.Items)
            //    {
            //        if (((DimensionLevel)obj).ID == acitem.DimensionLevel.ID)
            //        {
            //            acitem.DimensionLevel.Display = ((DimensionLevel)obj).Display;
            //            break;
            //        }
            //    }
            //    //Parent 
            //    if (xn.Attributes["Parent"].InnerText.Trim() == "")
            //    {
            //        acitem.Parent = null;
            //    }
            //    else
            //    {
            //        acitem.Parent = new ACItem();
            //        acitem.Parent.ID = xn.Attributes["Parent"].InnerText;
            //    }
            //    this.ACItems.Items.Add(acitem);
            //}

            ////完整填充Parent
            //for (int i = 0; i < this.ACItems.Count; i++)
            //{
            //    for (int j = 0; j < this.ACItems.Count; j++)
            //    {
            //        if (((ACItem)this.ACItems.Items[j]).Parent != null && ((ACItem)this.ACItems.Items[j]).Parent.ID == ((ACItem)this.ACItems.Items[i]).ID)
            //        {
            //            ((ACItem)this.ACItems.Items[j]).Parent = (ACItem)this.ACItems.Items[i];
            //        }
            //    }
            //}
            #endregion
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 Dimension 对象是否等效于当前 Dimension
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            Dimension dimension = null;
            try
            {
                dimension = (Dimension)obj;
            }
            catch
            {
                return false;
            }
            bool result = true;
            if (this.ID != dimension.ID)
            {
                result = false;
            }
            else if (this.Display != dimension.Display)
            {
                result = false;
            }
            else if (this.Hierachical != dimension.Hierachical)
            {
                result = false;
            }
            else if (this.MultiCheck != dimension.MultiCheck)
            {
                result = false;
            }
            else if (this.ACSection.ID!=dimension.ACSection.ID)
            {
                result = false;
            }
            else if (!this.DimensionLevels.Equals(dimension.DimensionLevels))
            {
                result = false;
            }
            else if (!this.ACItems.Equals(dimension.ACItems))
            {
                result = false;
            }
            return result;
        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
    }
}
